﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Platformer
{
    class MessageInbox
    {

        public Rectangle HUDBounds { get; set; } //The upper left corner of the drawing area of the HUD
        public Vector2 TextOffset = new Vector2(150, 10); //Offset from the HUDBounds

        public GUIButton PreviousButton, NextButton;

        public MessageInbox()
        {
            NextButton = new GUIButton();
            NextButton.Bounds = new Rectangle(0, 0, 15, 15);
            NextButton.OnClick += NextScreen;
            PreviousButton = new GUIButton();
            PreviousButton.OnClick += PreviousScreen;
            PreviousButton.Bounds = new Rectangle(0, 0, 15, 15);

            Messages = new ArrayList();
        }

        public ArrayList Messages { private set; get; }

        public bool HasMessage
        {
            get
            {
                return Messages.Count != 0;
            }
        }

        private int _currentMessageIndex;
        public Message CurrentMessage
        {
            get
            {
                if (_currentMessageIndex < 0) return null;
                return (Message)Messages[_currentMessageIndex];
            }
        }

        public bool IsFirstMessage { get { return _currentMessageIndex == 0; } }
        public bool IsLastMessage { get { return _currentMessageIndex == Messages.Count - 1; } }

        public void Update(GameTime gameTime)
        {
            PreviousButton.Update(gameTime);
            NextButton.Update(gameTime);
        }

        public void AddMessage(Message m)
        {
            Messages.Add(m);
        }

        public void NextScreen()
        {
            if (!CurrentMessage.LastScreen)
            {
                CurrentMessage.NextScreen();
            }
            else if(!IsLastMessage)
            {
                _currentMessageIndex++;
            }
            else if (IsLastMessage && CurrentMessage.LastScreen)
            {
                Messages.Clear();
                _currentMessageIndex = 0;
            }
        }
        public void PreviousScreen()
        {
            if (!CurrentMessage.FirstScreen)
            {
                CurrentMessage.PreviousScreen();
            }
            else if (!IsFirstMessage)
            {
                _currentMessageIndex--;
            }
        }

        public void Draw(SpriteBatch sb)
        {
            Vector2 stringPosition = new Vector2(HUDBounds.X, HUDBounds.Y) + TextOffset;
            sb.DrawString(Message.MESSAGE_FONT, CurrentMessage.SenderName + ":", stringPosition,Color.Yellow);
            stringPosition.Y += Message.LINE_SPACING_PIXELS;
            foreach (String s in CurrentMessage.CurrentScreen)
            {
                sb.DrawString(Message.MESSAGE_FONT, s, stringPosition, Color.Yellow);
                stringPosition.Y += Message.LINE_SPACING_PIXELS;
            }

            PreviousButton.Position = new Vector2(HUDBounds.X, HUDBounds.Y) +
                new Vector2(stringPosition.X + Message.MAX_HORIZONTAL_SIZE + 80,TextOffset.Y+10);
            NextButton.Position = PreviousButton.Position + new Vector2(0, 50);
            NextButton.Draw(sb);
            PreviousButton.Draw(sb);
        }





    }
}
